package adv42;

import java.util.concurrent.*;
import java.util.*;
/**
 * @author oldliu
 * @since 1.0
 */
public class XianchengChi {
    public static void main(String[] args) {
        ExecutorService es= Executors.newFixedThreadPool(10);
        System.out.println(es.getClass().getName());
        List<Future<Long>> list=new ArrayList<>();

        for (int i = 10000; i <300000 ; i+=10000) {
            // CalcSum c=new CalcSum(i,i+10000);
            Future<Long> submit = es.submit(new CalcSum(i, i + 10000));
            list.add(submit);
        }
        long sum=0;
        System.out.println("开始等待结果！");
        for (Future<Long> future : list) {
            try {
                sum+=future.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        System.out.println("和是："+sum);
        es.shutdown();
    }
}
//分段计算数据和
 class CalcSum implements Callable<Long>{
    private final int start;
    private final int end;

    public CalcSum(int start, int end){
        this.start=start;
        this.end=end;
    }
    @Override
    public Long call() throws Exception {
        long t=0;
        for (int i = start; i < end; i++) {
            t+=i;
            if(i%21==0)
                Thread.sleep(1);
        }
        System.out.println(Thread.currentThread().getName()+"得到了结果："+t);
        return t;
    }
}
